<!DOCTYPE html>
<html>
<head></head><body>
<h3><span class="mw-headline" id="Python"><span class="Apple-style-span" style="font-size: large;">Python</span></span></h3>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="python source-python" style="font-family: monospace;">
<pre class="de1"><span class="kw1"><span class="Apple-style-span" style="font-size: large;">class</span></span><span class="Apple-style-span" style="font-size: large;"> Singleton</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="kw2"><span class="Apple-style-span" style="font-size: large;">type</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="Apple-style-span" style="font-size: large;">:
    </span><span class="kw1"><span class="Apple-style-span" style="font-size: large;">def</span></span><span class="Apple-style-span" style="font-size: large;"> </span><span class="kw4"><span class="Apple-style-span" style="font-size: large;">__init__</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="Apple-style-span" style="font-size: large;">cls, name, bases, </span><span class="kw2"><span class="Apple-style-span" style="font-size: large;">dict</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="Apple-style-span" style="font-size: large;">:
        </span><span class="kw2"><span class="Apple-style-span" style="font-size: large;">super</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="Apple-style-span" style="font-size: large;">Singleton, cls</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="Apple-style-span" style="font-size: large;">.</span><span class="kw4"><span class="Apple-style-span" style="font-size: large;">__init__</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="Apple-style-span" style="font-size: large;">name, bases, </span><span class="kw2"><span class="Apple-style-span" style="font-size: large;">dict</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="Apple-style-span" style="font-size: large;">
        cls.</span><span class="me1"><span class="Apple-style-span" style="font-size: large;">instance</span></span><span class="Apple-style-span" style="font-size: large;"> = </span><span class="kw2"><span class="Apple-style-span" style="font-size: large;">None</span></span><span class="Apple-style-span" style="font-size: large;">
 
    </span><span class="kw1"><span class="Apple-style-span" style="font-size: large;">def</span></span><span class="Apple-style-span" style="font-size: large;"> </span><span class="kw4"><span class="Apple-style-span" style="font-size: large;">__call__</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="Apple-style-span" style="font-size: large;">cls, </span><span class="sy0"><span class="Apple-style-span" style="font-size: large;">*</span></span><span class="Apple-style-span" style="font-size: large;">args, </span><span class="sy0"><span class="Apple-style-span" style="font-size: large;">**</span></span><span class="Apple-style-span" style="font-size: large;">kw</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="Apple-style-span" style="font-size: large;">:
        </span><span class="kw1"><span class="Apple-style-span" style="font-size: large;">if</span></span><span class="Apple-style-span" style="font-size: large;"> cls.</span><span class="me1"><span class="Apple-style-span" style="font-size: large;">instance</span></span><span class="Apple-style-span" style="font-size: large;"> </span><span class="kw1"><span class="Apple-style-span" style="font-size: large;">is</span></span><span class="Apple-style-span" style="font-size: large;"> </span><span class="kw2"><span class="Apple-style-span" style="font-size: large;">None</span></span><span class="Apple-style-span" style="font-size: large;">:
            cls.</span><span class="me1"><span class="Apple-style-span" style="font-size: large;">instance</span></span><span class="Apple-style-span" style="font-size: large;"> = </span><span class="kw2"><span class="Apple-style-span" style="font-size: large;">super</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="Apple-style-span" style="font-size: large;">Singleton, cls</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="Apple-style-span" style="font-size: large;">.</span><span class="kw4"><span class="Apple-style-span" style="font-size: large;">__call__</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="sy0"><span class="Apple-style-span" style="font-size: large;">*</span></span><span class="Apple-style-span" style="font-size: large;">args, </span><span class="sy0"><span class="Apple-style-span" style="font-size: large;">**</span></span><span class="Apple-style-span" style="font-size: large;">kw</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="Apple-style-span" style="font-size: large;">
 
        </span><span class="kw1"><span class="Apple-style-span" style="font-size: large;">return</span></span><span class="Apple-style-span" style="font-size: large;"> cls.</span><span class="me1"><span class="Apple-style-span" style="font-size: large;">instance</span></span><span class="Apple-style-span" style="font-size: large;">
 
</span><span class="kw1"><span class="Apple-style-span" style="font-size: large;">class</span></span><span class="Apple-style-span" style="font-size: large;"> MyClass</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="kw2"><span class="Apple-style-span" style="font-size: large;">object</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="Apple-style-span" style="font-size: large;">:
    </span><span class="kw4"><span class="Apple-style-span" style="font-size: large;">__metaclass__</span></span><span class="Apple-style-span" style="font-size: large;"> = Singleton
 
</span><span class="kw1"><span class="Apple-style-span" style="font-size: large;">print</span></span><span class="Apple-style-span" style="font-size: large;"> MyClass</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="Apple-style-span" style="font-size: large;">
</span><span class="kw1"><span class="Apple-style-span" style="font-size: large;">print</span></span><span class="Apple-style-span" style="font-size: large;"> MyClass</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="Apple-style-span" style="font-size: large;">
</span></pre>
</div>
</div>
<h3 id="g0.0.1"><span class="Apple-style-span" style="font-size: large;">Python (using decorators)</span></h3>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="python source-python" style="font-family: monospace;">
<pre class="de1"><span class="kw1"><span class="Apple-style-span" style="font-size: large;">def</span></span><span class="Apple-style-span" style="font-size: large;"> singleton</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="Apple-style-span" style="font-size: large;">cls</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="Apple-style-span" style="font-size: large;">:
    instance_container = </span><span class="br0"><span class="Apple-style-span" style="font-size: large;">[</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">]</span></span><span class="Apple-style-span" style="font-size: large;">
    </span><span class="kw1"><span class="Apple-style-span" style="font-size: large;">def</span></span><span class="Apple-style-span" style="font-size: large;"> getinstance</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="Apple-style-span" style="font-size: large;">:
        </span><span class="kw1"><span class="Apple-style-span" style="font-size: large;">if</span></span><span class="Apple-style-span" style="font-size: large;"> </span><span class="kw1"><span class="Apple-style-span" style="font-size: large;">not</span></span><span class="Apple-style-span" style="font-size: large;"> </span><span class="kw2"><span class="Apple-style-span" style="font-size: large;">len</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="Apple-style-span" style="font-size: large;">instance_container</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="Apple-style-span" style="font-size: large;">:
            instance_container.</span><span class="me1"><span class="Apple-style-span" style="font-size: large;">append</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="Apple-style-span" style="font-size: large;">cls</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">(</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="br0"><span class="Apple-style-span" style="font-size: large;">)</span></span><span class="Apple-style-span" style="font-size: large;">
        </span><span class="kw1"><span class="Apple-style-span" style="font-size: large;">return</span></span><span class="Apple-style-span" style="font-size: large;"> instance_container</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">[</span></span><span class="Apple-style-span" style="font-size: large;">0</span><span class="br0"><span class="Apple-style-span" style="font-size: large;">]</span></span><span class="Apple-style-span" style="font-size: large;">
    </span><span class="kw1"><span class="Apple-style-span" style="font-size: large;">return</span></span><span class="Apple-style-span" style="font-size: large;"> getinstance
 
@singleton
</span><span class="kw1"><span class="Apple-style-span" style="font-size: large;">class</span></span><span class="Apple-style-span" style="font-size: large;"> MyClass:
    ...
</span></pre>
</div>
</div>
<p><span class="Apple-style-span" style="font-size: large;">Note that we have to use `instance_container` instead of a simple 
variable because otherwise we would have to assign this variable 
directly in the inner function which would make it local to the inner 
function. See </span><a href="http://www.python.org/dev/peps/pep-3104/" class="external text" rel="nofollow"><span class="Apple-style-span" style="font-size: large;">PEP-3104</span></a><span class="Apple-style-span" style="font-size: large;"> for more extensive explanation. Note python 2.6+</span></p>


</body>
</html>
